package com.gome.wechat.program.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gome.wechat.program.dto.LongResponseDto;
import com.gome.wechat.program.dto.UserDto;
import com.gome.wechat.program.service.applicationservice.UserApplicationService;
import com.gome.wechat.program.service.queryservice.UserQueryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by liwei113 on 2017/10/20 0020.
 */
@Api("用户")
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserController {

    @Autowired
    UserApplicationService userApplicationService;

    @Autowired
    UserQueryService userQueryService;

    @ApiOperation(value = "保存用户",response = LongResponseDto.class, notes = "开发by李巍")
    @PostMapping("/save")
    public LongResponseDto saveUser(@RequestBody UserDto userDto) {
        UserDto result = userApplicationService.saveOrUpdateUser(userDto);
        return new LongResponseDto(result.getId());
    }

    @ApiOperation(value = "通过用户ID获取用户",response = UserDto.class, notes = "开发by李巍")
    @GetMapping("/openid")
    public UserDto getUserById(@RequestParam String  openid) {
       UserDto userDto =  userQueryService.findByOpenId(openid);
        return userDto;
    }
}
